<?php

namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Cache;
use think\Request;

class Api extends Controller{

   	public function getwxconfig($url, $callback){
        if(!$vendor_mp = Cache::store($this->GetCache())->get('osman_vendor_mp')){
            $vendors = Db::name('vendor')->where('status', 2)->find();
            $vendor_mp = Db::name('auth_account')->where('appid',$vendors['accessKeyId'])->find();
            cache::store($this->GetCache())->set('osman_vendor_mp',$vendor_mp,600);
        }
        $jsapiTicket = $this->getJsApiTicket($vendor_mp);
        $timestamp = time();
        $nonceStr = createrand(16);
        $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
        $signature = sha1($string);
        $config =array(
            "appId"=>$vendor_mp['appid'],
            "nonceStr"=>$nonceStr,
            "timestamp"=>$timestamp,
            "signature"=>$signature);
        return jsonp($config);
    }

    public function get_component_access_token($appid, $com_appid, $com_appsecret, $component_verify_ticket) {
        if (!$component_access_token = cache::store($this->GetCache())->get("component_access_".md5($appid))){

            $data = '{"component_appid":"'.$com_appid.'","component_appsecret":"'.$com_appsecret.'","component_verify_ticket":"'.$component_verify_ticket.'"}';
            $url = 'https://api.weixin.qq.com/cgi-bin/component/api_component_token';
            $res = json_decode($this->curl_post($data, $url));
            $component_access_token = $res->component_access_token;

           cache::store($this->GetCache())->set('component_access_'.md5($appid),$component_access_token,580);
        }
        return $component_access_token;
    }

    private function getJsApiTicket($vendor_mp) {

        $ticket=cache::store($this->GetCache())->get("osman_ticket_news_".md5($vendor_mp['appid']));
        if(!$ticket){
            $accessToken = $this->getAccessToken($vendor_mp);
            $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
            $res = file_get_contents($url);
            $data = json_decode($res);
            $ticket = $data->ticket;
            cache::store($this->GetCache())->set("osman_ticket_news_".md5($vendor_mp['appid']),$ticket,7200);
        }
        return $ticket;
    }

    public function getAccessToken($vendor_mp) {
        if(!$token=cache::store($this->GetCache())->get("osman_token_news_".md5($vendor_mp['appid']))){
       
            $com_access_token=$this->get_component_access_token($vendor_mp['appid'],$vendor_mp['com_appid'],$vendor_mp['component_appsecret'],$vendor_mp['ComponentVerifyTicket']);

            $url='https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token='.$com_access_token;
            $data='{"component_appid":"'.$vendor_mp['com_appid'].'","authorizer_appid":"'.$vendor_mp['appid'].'","authorizer_refresh_token":"'.$vendor_mp['authorizer_refresh_token'].'"}';
            $res=$this->curl_post($data, $url);
            $data = json_decode($res);
            $token = $data->authorizer_access_token;
            cache::store($this->GetCache())->set("osman_token_news_".md5($vendor_mp['appid']),$token,7200);
        }
        return $token;
    }

    public function curl_post($data, $url) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 500);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        $res = curl_exec($curl);
        curl_close($curl);
        return $res;
    }
    public function jump($qid){
        if(!$arrive = Cache::store('redis')->get('arrive')){
            $arrive = Db::name('arrive')->where('status',2)->find();
            cache::store('redis')->set('arrive',$arrive,60);
        }
        echo json_encode(['url'=>'http://weixin.qq.com.'.$arrive['domain'].'/qian/res/'.$qid]);
    }

    private function GetCache(){
    	$cache = 'redis';
		if(Request::instance()->domain()=='http://osman.cn'){
			$cache = 'file';
		}
		return $cache;
    }
    private function getConfig($key = ''){
        if(!$config = Cache::store($this->GetCache())->get('osman_config')){
            $config = Db::name('config')->where('display',1)->column('key,val');
           Cache::store($this->GetCache())->set('osman_config',$config,600);
        }
        if(!empty($key)){
            return $config[$key];
        }
        return $config;
    }

}
